﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DAL;

namespace PeisongWeb.admin
{
    public partial class customerEdit : adminPage
    {
        protected int companyId
        {
            get
            {
                int tmp;
                try { tmp = Convert.ToInt32(ViewState["companyId"]); }
                catch { tmp = 0; }
                return tmp;
            }
            set
            { ViewState["companyId"] = value; }
        }

        protected int customerId
        {
            get
            {
                int tmp;
                try { tmp = Convert.ToInt32(ViewState["customerId"]); }
                catch { tmp = 0; }
                return tmp;
            }
            set
            { ViewState["customerId"] = value; }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (hasPermission("FC", 2))
                {
                    if (Request.QueryString["company"] != null)
                    {
                        if (Library.Number.IsIntPositive(Request.QueryString["company"]))
                        {
                            companyId = Convert.ToInt32(Request.QueryString["company"]);
                        }
                    }
                    if (Request.QueryString["customer"] != null)
                    {
                        if (Library.Number.IsIntPositive(Request.QueryString["customer"]))
                        {
                            customerId = Convert.ToInt32(Request.QueryString["customer"]);
                        }
                    }
                    else
                    {
                        li_updatepwd.Visible = false;
                    }
                    if (companyId != 0)
                    {
                        if (logic.company.getById(companyId).buyerType == "2")
                        {
                            li_expireDt.Visible = true;
                            ext_expireDt.Value = DateTime.Now.AddDays(7).ToString();
                        }
                        else
                        {
                            li_expireDt.Visible = false;
                            ext_expireDt.Value = DateTime.Now.ToString();
                        }
                    }
                    if (customerId != 0)
                        getDetails();
                    getTitle(companyId);
                }
                else
                {
                    Library.Script.ClientMsgUrl("没有权限！", "index.aspx");
                    return;
                }
            }
        }
        private void getDetails()
        {
            CustomerEntity cus = logic.customer.getById(customerId);
            txt_Username.Text = cus.Username;
            txt_Username.ReadOnly = true;
            hid_Pwd.Value = cus.Password;

            companyId = cus.CompanyId;

            txt_Realname.Text = cus.Realname;
            txt_Phone.Text = cus.Phone;
            txt_Email.Text = cus.Email;
            chx_isAdmin.Checked = cus.IsAdmin;
            chx_isRefuse.Checked = cus.IsRefuse;
            if (logic.company.getById(companyId).buyerType == "2")
            {
                li_expireDt.Visible = true;
            }
            else
            {
                li_expireDt.Visible = false;
            }
            ext_expireDt.Value = cus.expireDt;
        }

        void getTitle(int _companyId)
        {
            panel_Title.Title = logic.company.getById(companyId).Company+" - 用户编辑";
        }

        protected void lbt_updatePwd_Click(object sender, EventArgs e)
        {
            string pwd = txt_Pwd.Text;
            if (pwd.Length>0)
            {
                logic.customer.updatePwdByadmin(customerId, Library.StringLbl.EncryptMD5(pwd),customerId,GetClientIP());
                Library.Script.ClientMsg("密码修改成功！");
            }
            else
                Library.Script.ClientMsg("密码不能为空！");
        }

        protected void btn_Save_Click(object sender, EventArgs e)
        {
            if (customerId==0)
            {
                if (logic.customer.isExistUser(txt_Username.Text.Trim()))
                {
                    Library.Script.ClientMsg("此用户名已存在，请重新输入！");
                    return;
                }
            }

            CustomerEntity cus = new CustomerEntity();

            cus.Username = txt_Username.Text;
            cus.Email = txt_Email.Text;
            cus.Phone = txt_Phone.Text;
            cus.Realname = txt_Realname.Text;
            cus.CompanyId = companyId;
            cus.IsRefuse = chx_isRefuse.Checked;
            cus.IsAdmin = chx_isAdmin.Checked;
            //cus.expireDt = Convert.ToDateTime(ext_expireDt.Value).AddHours(Convert.ToInt32(DateTime.Now.Hour));
            cus.expireDt = Convert.ToDateTime(ext_expireDt.Value).AddHours(Convert.ToInt32(DateTime.Now.Hour)).AddMinutes(Convert.ToInt32(DateTime.Now.Minute)).AddSeconds(Convert.ToInt32(DateTime.Now.Second));
            if (customerId != 0)
            {

                cus.CustomerId = customerId;
                cus.Password = hid_Pwd.Value;
                logic.customer.update(cus);
                
            }
            else
            {

                cus.Password = Library.StringLbl.EncryptMD5(hid_Pwd.Value); 
                logic.customer.insert(cus); 
            }
            Library.Script.ClientMsgUrl("保存成功！", "customerList.aspx?company=" + companyId);

        }
    }
}
